// Figure 8: Specification Chart by Spending Component //

clear all
cap log close

global path ""
use "$path/Main.dta", clear

cap program drop specchart
program specchart
syntax varlist, [replace] spec(string)
	* save current data
	tempfile temp
	save "`temp'",replace
	*dataset to store estimates
	if "`replace'"!=""{
			clear
			gen beta=.
			gen se=.
			gen spec_id=.
			gen u95=.
			gen u90=.
			gen l95=.
			gen l90=.
			save "estimates.dta",replace
	}	
	else{
		* load dataset
		use "estimates.dta",clear
	}
	* add observation
	local obs=_N+1
	set obs `obs'
	replace spec_id=`obs' if _n==`obs'
	* store estimates
	replace beta =_b[`varlist'] if  spec_id==`obs'
	replace se=_se[`varlist']   if  spec_id==`obs'
	replace u95=beta+invt(e(df_r),0.975)*se if  spec_id==`obs'
	replace u90=beta+invt(e(df_r),0.95)*se if  spec_id==`obs'
	replace l95=beta-invt(e(df_r),0.975)*se  if  spec_id==`obs'
	replace l90=beta-invt(e(df_r),0.95)*se  if  spec_id==`obs'
	* store specification
	foreach s in `spec'{
		cap gen `s'=1 			if  spec_id==`obs'
		cap replace `s'=1 		 if  spec_id==`obs'
	}
		save "estimates.dta",replace
	* restore dataset
	use `temp',clear
end

capture reg log_pc_x_totexp win_M win_VM win_G win_VG win_CONG   
specchart win_VM,spec(FE_no FCN_no) replace

foreach y in totexp pubsrv socsrv econsrv educ health emp hsing debtsrv {

local FCN_no ""
local FCN_linear "mv_*_1 dmv_*_1"
local FCN_quadratic "mv_*_1 dmv_*_1 mv_*_2 dmv_*_2"

foreach r in no linear quadratic {
xtset idcode

capture reg log_pc_x_`y' win_M win_VM win_G win_VG win_CONG `FCN_`r'', robust cluster(idcode_prov)
specchart  win_VM,spec(Y_`y' FCN_`r') 

capture reg log_pc_x_`y' win_M win_VM win_G win_VG win_CONG `FCN_`r'' i.year, robust cluster(idcode_prov)
specchart  win_VM,spec(Y_`y' FE_year FCN_`r') 

capture xi: xtreg log_pc_x_`y' win_M win_VM win_G win_VG win_CONG `FCN_`r'', fe robust cluster(idcode_prov)
specchart  win_VM,spec(Y_`y' FE_mun FCN_`r') 

capture xi: xtreg log_pc_x_`y' win_M win_VM win_G win_VG win_CONG `FCN_`r'' i.year, fe robust cluster(idcode_prov)
specchart  win_VM,spec(Y_`y' FE_year FE_mun FCN_`r')

}
}

use "estimates.dta", clear
drop if _n == 1
replace spec_id = spec_id - 1

* drop duplicates 
duplicates drop Y_totexp Y_pubsrv Y_socsrv Y_econsrv Y_educ Y_health Y_emp Y_hsing Y_debtsrv FE_year FE_mun FCN_no FCN_linear FCN_quadratic, force

* drop specifications
drop if Y_emp == 1
drop if Y_totexp == 1

gsort -Y_debtsrv -Y_hsing -Y_health -Y_educ -Y_econsrv -Y_socsrv -Y_pubsrv ///
	-FE_mun -FE_year ///
	-FCN_quadratic -FCN_linear -FCN_no, mfirst

gen rank=_n

	local scoff=" "
	local scon=" "
	local ind=-1.817
	foreach var in FE_year FE_mun FCN_no FCN_linear FCN_quadratic {
	   cap gen i_`var'=`ind'
	   local ind=`ind'-0.1
	   
	   local scoff="`scoff' (scatter i_`var' rank,msize(vsmall) mcolor(gs10))" 
	   local scon="`scon' (scatter i_`var' rank if `var'==1,msize(vsmall) mcolor(black))"
	   
	   if "`var'" == "Y_debtsrv" | "`var'" == "FE_mun" {
	   local ind=`ind'-0.3
	   }
	}

gen zero = 0

tw (rbar u95 l95 rank, fcolor(gs12) lcolor(gs12) lwidth(none)) /// 95% CI
   (rbar u90 l90 rank, fcolor(gs6) lcolor(gs16) lwidth(none)) /// 90% CI
   (scatter beta rank, mcolor(black) msymbol(d) msize(*0.2)) ///  point estimates
   (rbar u95 l95 rank if Y_pubsrv == 1 | Y_econsrv == 1 | Y_health == 1 | Y_debt == 1, fcolor(gs14) lcolor(gs14) lwidth(none)) /// 95% CI
   (rbar u90 l90 rank if Y_pubsrv == 1 | Y_econsrv == 1 | Y_health == 1 | Y_debt == 1, fcolor(gs11) lcolor(gs16) lwidth(none)) /// 90% CI
   (scatter beta rank if Y_pubsrv == 1 | Y_econsrv == 1 | Y_health == 1 | Y_debt == 1, mcolor(gs3) msymbol(d) msize(*0.2)) ///  point estimates
   (line zero rank, lcolor(red) lpattern(shortdash)) /// zero line
   `scoff' `scon' /// indicators for spec
   , legend(order(3 "Point Estimate" 1 "95% CI" 5 "90% CI" 2 "90% CI") region(lcolor(white)) ///
	pos(6) ring(1) rows(1) size(vsmall) symysize(small) symxsize(small)) ///
   xtitle(" ") ytitle(" ") title("Independent Variable: Horizontal Dynasty w/ Vice Mayor", size(medsmall)) ///
   yscale(noline) xscale(noline) ylab(-1(0.5)1,noticks nogrid angle(horizontal) labsize(vsmall)) xlab("", noticks)  ///
   graphregion (fcolor(white) lcolor(white)) plotregion(fcolor(white) lcolor(white)) ///
   text(-1.4 6.5 "Public" "Services", size(vsmall)) text(-1.4 18.5 "Social" "Services", size(vsmall)) text(-1.4 30.5 "Economic" "Services", size(vsmall)) ///
   text(-1.4 42.5 "Education", size(vsmall)) text(-1.4 54.5 "Health", size(vsmall)) text(-1.4 66.5 "Housing", size(vsmall)) text(-1.4 78.5 "Debt" "Services", size(vsmall)) ///
   text(-2.85 15.5 "{&bullet}", size(medlarge)) text(-2.85 16.5 "Included in the Regression", placement(e) size(vsmall)) ///
   text(-2.85 42.5 "{&bullet}", color(gs10) size(medlarge)) text(-2.85 43.5 "Excluded from the Regression", placement(e) size(vsmall))

gr_edit .yaxis1.add_ticks -1.4 `"Dependent Variable"', custom tickset(major) editstyle(tickstyle(textstyle(size(small))))

gr_edit .yaxis1.add_ticks -1.7 `"Fixed Effects"', custom tickset(major) editstyle(tickstyle(textstyle(size(small))) )
gr_edit .yaxis1.add_ticks -1.8 `"Year"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -1.9 `"Municipality"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )

gr_edit .yaxis1.add_ticks -2.2 `"Function"', custom tickset(major) editstyle(tickstyle(textstyle(size(small))) )
gr_edit .yaxis1.add_ticks -2.3 `"None"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -2.4 `"Linear"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -2.5 `"Quadratic"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )

gr_edit .yaxis1.add_ticks 1.2 `"Coefficient"', custom tickset(major) editstyle(tickstyle(textstyle(size(small))) )
